<?php


namespace app\admin\controller;


use app\admin\base\JwBase;
use think\Exception;
use think\facade\Db;
use think\facade\Validate;

class Admin extends JwBase
{
    /**
     * info: 管理员列表
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 22:48
     * Url: admins
     */
    public function admins()
    {

        $postData = input();

        $limit = input('limit', 10);
        $page = (input('page', 1) - 1) * $limit;

        $order = ['aid' => 'desc', 'a.addtime' => 'desc'];

        $where['is_del'] = 1;

        $table = Db::table('jw_admin');
        if (isset($postData['query']) && !empty($postData['query'])) {
            $table->where('account|mobile', 'like', '%' . $postData['query'] . '%');
        }
        $userlist = $table
            ->alias('a')
            ->leftJoin('jw_role r ', 'a.role= r.rid')
            ->limit($page, $limit)
            ->field("aid,account,mobile,status,a.lasttime as lasttime,last_ip,a.role,name as roleName,login")
            ->order($order)
            ->select();

        $data['count'] = Db::table('jw_admin')
            ->count('aid');
        $data['list'] = $userlist;

        $this->respInfo(1000, $data);
    }

    /**
     * info: 修改用户状态
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 21:49
     * Url: setAdminRole
     */
    public function setAdminStatus()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'aid|操作ID' => ['require', 'number', 'max' => 10, 'min' => 1],
                    'status|状态' => ['require', 'max' => 1, 'min' => 1],

                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                $dataAdmin["status"] = $postData['status'];

                $role = \app\model\Admin::find($postData['aid']);
                $role->save($dataAdmin);
                $this->respInfo(1000);

            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 管理员详情
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 23:58
     * Url: getAdminInfo
     */
    public function getAdminInfo()
    {
        $aid = input('aid');
        $data = \app\model\Admin::find($aid)->hidden(['password']);
        $this->respInfo(1000, $data);
    }

    /**
     * info: 管理员添加
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 22:48
     * Url: addAdmin
     */
    public function addAdmin()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'account|账号' => ['require', 'max' => 15, 'min' => 6],
                    'password|密码' => ['require', 'max' => 15, 'min' => 6],
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }


                $dataAdmin["account"] = $postData['account'];
                $dataAdmin["password"] = $postData['password'];
                if (request()->has("mobile", 'post', true)) {
                    $dataAdmin["mobile"] = $postData['mobile'];
                }

                $admin = new \app\model\Admin();;
                $admin->save($dataAdmin);
                if ($admin->aid > 0) {
                    $this->respInfo(1000);
                } else {
                    $this->respInfo(3000);
                }

            } else {
                $this->respInfo(2001);
            }
        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 管理员修改
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 22:49
     * Url: editAdmin
     */
    public function editAdmin()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'aid|操作ID' => ['require', 'max' => 10, 'min' => 1],
                    'mobile|手机号' => ['require', 'mobile'],
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                $dataAdmin["mobile"] = $postData['mobile'];
                if (request()->has("password", 'post', true)) {
                    $dataAdmin["password"] = $postData['password'];
                }

                $admin = \app\model\Admin::find($postData['aid']);
                $admin->save($dataAdmin);
                $this->respInfo(1000);

            } else {
                $this->respInfo(2001);
            }
        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 管理员删除
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 22:49
     * Url: delAdmin
     */
    public function delAdmin()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                if (isset($postData['aid']) && !empty($postData['aid'])) {
                    $data = \app\model\Admin::find($postData['aid']);
                    $data->delete();
                    $this->respInfo(1000);
                } else {
                    $this->respInfo(2500, [], '参数错误');
                }
            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 获取角色列表
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 23:34
     * Url: getRole
     */
    public function getRole()
    {
        $data = \app\model\Role::getRoleAll();
        $this->respInfo(1000, $data);
    }

    /**
     * info: 保存角色
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/10 23:35
     * Url: saveAdminRole
     */
    public function saveAdminRole()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'aid|操作ID' => ['require', 'max' => 10, 'min' => 1],
                    'rid|角色' => ['require', 'number', 'max' => 10, 'min' => 1],
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                $dataAdmin["role"] = $postData['rid'];

                $admin = \app\model\Admin::find($postData['aid']);
                $admin->save($dataAdmin);
                $this->respInfo(1000);

            } else {
                $this->respInfo(2001);
            }
        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }
}